package cn.iocoder.yudao.module.member.controller.admin.praxisProjectResult;

import cn.hutool.core.lang.Assert;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.controller.admin.praxisProjectResult.vo.*;
import cn.iocoder.yudao.module.member.convert.praxisProjectResult.PraxisProjectResultConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.praxisProjectResult.PraxisProjectResultDO;
import cn.iocoder.yudao.module.member.service.praxisProjectResult.PraxisProjectResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 项目结果公示")
@RestController
@RequestMapping("/member/praxis-result")
@Validated
public class PraxisProjectResultController {

    @Resource
    private PraxisProjectResultService praxisProjectResultService;

    @PostMapping("/create")
    @ApiOperation("创建项目结果公示")
    public CommonResult<Long> createPraxisProjectResult(@Valid @RequestBody PraxisProjectResultCreateReqVO createReqVO) {
        Assert.notNull(createReqVO.getProjectId(), "项目不能为空");
        createReqVO.setStatus(0);
        createReqVO.setExamineStatus(null);
        createReqVO.setExamineFlag(0);
        createReqVO.setCreatorRole("平台运营");
        return success(praxisProjectResultService.createPraxisProjectResult(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新项目结果公示")
    @TenantIgnore
    public CommonResult<Boolean> updatePraxisProjectResult(@Valid @RequestBody PraxisProjectResultUpdateReqVO updateReqVO) {
        Assert.notNull(updateReqVO.getProjectId(), "项目不能为空");
        updateReqVO.setReleaseTime(new Date());
        if(null==updateReqVO.getStatus()|| 0==updateReqVO.getStatus()){
            updateReqVO.setStatus(0);
            updateReqVO.setExamineStatus(null);
            updateReqVO.setExamineFlag(0);
        }
        praxisProjectResultService.updatePraxisProjectResult(updateReqVO);
        return success(true);
    }


    @PutMapping("/release")
    @ApiOperation("发布项目结果公示")
    public CommonResult<Boolean> release(@Valid @RequestBody PraxisProjectResultUpdateReqVO updateReqVO) {
        praxisProjectResultService.release(updateReqVO);
        return success(true);
    }

    @TenantIgnore
    @PutMapping("/examine")
    @ApiOperation("审核结果公示")
    public CommonResult<Boolean> examinePraxisProjectResult(@Valid @RequestBody PraxisProjectResultUpdateVO updateReqVO) {
        praxisProjectResultService.examinePraxisProjectResult(updateReqVO);
        return success(true);
    }

    @TenantIgnore
    @DeleteMapping("/delete")
    @ApiOperation("删除项目结果公示")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    public CommonResult<Boolean> deletePraxisProjectResult(@RequestParam("id") Long id) {
        praxisProjectResultService.deletePraxisProjectResult(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得项目结果公示")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<PraxisProjectResultRespVO> getPraxisProjectResultById(@RequestParam("id") Long id) {
        PraxisProjectResultRespVO p = praxisProjectResultService.getPraxisProjectResultById(id);
        return success(p);
    }


    @PostMapping("/page")
    @ApiOperation("获得项目结果公示分页")
    @TenantIgnore
    public CommonResult<PageResult<PraxisProjectResultRespVO>> getPraxisProjectResultPage(@Valid  @RequestBody  PraxisProjectResultPageReqVO pageVO) {
        PageResult<PraxisProjectResultRespVO> pageResult = praxisProjectResultService.getPraxisProjectResultPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/export")
    @ApiOperation("导出项目结果公示 Excel")
    public void exportPraxisProjectResultExcel(@Valid PraxisProjectResultExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PraxisProjectResultDO> list = praxisProjectResultService.getPraxisProjectResultList(exportReqVO);
        // 导出 Excel
        List<PraxisProjectResultExcelVO> datas = PraxisProjectResultConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "项目结果公示.xls", "数据", PraxisProjectResultExcelVO.class, datas);
    }

}
